<?php
namespace baidu;
class BaiduKline
{

    public static $info_url ="https://finance.pae.baidu.com/vapi/v1/getquotation?group=huilv_minute&need_reverse_real=1&code=%sUSD&finClientType=pc";
    public static $min_url ="https://finance.pae.baidu.com/vapi/v1/getquotation?group=huilv_kline&ktype=min%d&code=%sUSD";
    public static $day_url = "https://finance.pae.baidu.com/vapi/v1/getquotation?group=huilv_kline&ktype=%s&code=%sUSD&finClientType=pc";
    public function httGet($url)
    {
        $ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36';// 伪造抓取 UA
        $ip = mt_rand(11, 191) . "." . mt_rand(0, 240) . "." . mt_rand(1, 240) . "." . mt_rand(1, 240);
        // 初始化cURL会话
        $ch = curl_init();
        // 设置cURL选项
        curl_setopt($ch, CURLOPT_URL, $url); // 设置请求的URL
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回响应而不是直接输出
        curl_setopt($ch, CURLOPT_HEADER, false); // 不返回HTTP头
        curl_setopt($ch, CURLOPT_REFERER, 'https://gushitong.baidu.com/');//模拟来路
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:' . $ip, 'CLIENT-IP:' . $ip));  //伪造IP
        curl_setopt($ch, CURLOPT_USERAGENT, $ua);// 伪造ua
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 允许重定向
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 关闭SSL证书验证（根据需要开启或关闭）
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 关闭SSL主机验证（根据需要开启或关闭）
        // 执行cURL会话
        $response = curl_exec($ch);
        // 检查是否有错误发生
        if (curl_errno($ch)) {
            $error_msg = curl_error($ch);
            curl_close($ch); // 关闭cURL会话
            return "cURL Error: $error_msg";
        }
        curl_close($ch);
        return $response;
    }
    public function getInfo($coin)
    {
        $url = sprintf(self::$info_url,strtoupper($coin));
        $data = $this->httGet($url);
        $res = json_decode($data,true);
        $data = $res["Result"]["cur"];
        if ($data["increase"] <0) {
            $tmp["open"] = "<span  class='fzmmm red'>{$data['price']}</span>";
            $tmp["change"] = "<span  class='fzmm bred' style='width: 70px;height: 30px;text-align: center;margin-top: 8px;display: -webkit-box;display: -ms-flexbox;display: flex;-webkit-box-pack: center;-ms-flex-pack: center;justify-content: center;-webkit-box-align: center;-ms-flex-align: center;align-items: center;font-size: 12px;color: #fff!important;float: right;border-radius: 3px; font-weight: 600; line-height: 25px; font-size: 14px;'>{$data['ratio']}</span>";
        } else {
            $tmp["open"] = "<span  class='fzmmm green'>{$data["price"]}</span>";
            $tmp["change"] = "<span  class='fzmm bgreen' style='width: 70px;height: 30px;text-align: center;margin-top: 8px;display: -webkit-box;display: -ms-flexbox;display: flex;-webkit-box-pack: center;-ms-flex-pack: center;justify-content: center;-webkit-box-align: center;-ms-flex-align: center;align-items: center;font-size: 12px;color: #fff!important;float: right;border-radius: 3px; font-weight: 600; line-height: 25px; font-size: 14px;'>+{$tmp["ratio"]}%</span>";
        }
        $tmp["updown_count"] = $data["increase"];
        $tmp["name"] = strtoupper($coin);
        return $tmp;
    }

    public function getList($coin,$type)
    {
        $url = sprintf(self::$day_url,$type,strtoupper($coin));
        $data = $this->httGet($url);
        $res = json_decode($data,true);
        $data =$res["Result"]["newMarketData"]["marketData"];
        $data= explode(";",$data);
        $arr =[];
        foreach ($data as $key=>$v) {
            $a  =explode(",",$v);
            $time = $a[1];
            $open = round($a[2],4);
            $close =round($a[3],4);
            $low = round($a[5],4);
            $high= round($a[4],4);
            $val =[$time,$open,$close,$low ,$high];
            $arr[$key] = $val;
        }
        $arr = array_reverse($arr);
        $list["data"] = array_slice($arr,0,200);
        $list["code"] = 0;
        return $list;
    }

    public function getMin($coin,$min)
    {
        $url = sprintf(self::$min_url,$min,strtoupper($coin));
        $data = $this->httGet($url);
        $res = json_decode($data,true);
        $data =$res["Result"]["newMarketData"]["marketData"];
        $data= explode(";",$data);
        $arr =[];
        foreach ($data as $key=>$v) {
            $a  =explode(",",$v);
            $time = $a[1];
            $open = round($a[2],4);
            $close =round($a[3],4);
            $low = round($a[5],4);
            $high= round($a[4],4);
            $val =[$time,$open,$close,$low ,$high];
            $arr[$key] = $val;
        }
        $arr = array_reverse($arr);
        $list["data"] = array_slice($arr,0,200);
        $list["code"] = 0;
        return $list;
    }

    public function getOne($coin)
    {
        $url = sprintf(self::$info_url,strtoupper($coin));
        $data = $this->httGet($url);
        $res = json_decode($data,true);
        $data = $res["Result"]["cur"];
        $now  = $res["Result"]["pankouinfos"]["list"];
        $open = $now[0]["value"];
        $high = $now[1]["value"];
        $price = $now[2]["value"];
        $close = $now[3]["value"];
        $low   = $now[4]["value"];
        $tmp["name"] = $coin; //名称
        $tmp["price"] =  $price; //当前价格
        $tmp["close"] = $close; //昨日收盘
        $tmp["open"] =  $open;//开盘价
        $tmp["high"] =  $high;//最高
        $tmp["low"] =  $low;//最低
        $chagne = $data["increase"];
        $updown = $chagne >0 ? "up" : "down";//涨还是跌
        if ($updown == "down") {
            $tmp["change"] = "<span  class='red' style='font-size:16px;'>{$chagne}</span>";
            $tmp["close"] = "<span  class='red' style='font-size:22px;'>{$close}</span";
        } else {
            $tmp["change"] = "<span  class='green' style='font-size:16px;'>{$change}</span>";
            $tmp["close"] = "<span  class='green' style='font-size:22px;'>{$close}</span";
        }
        return $tmp;
    }
}